/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "ti_msp_dl_config.h"

#include "stdio.h"

#include "delay.h"
#include "OLED/oled.h"
#include "jy61p/jy61p.h"
#include "bsp.h"
#include "usart/_usart.h"
#include "gpio/_gpio.h"
#include "key/key.h"
#include "motor/motor.h"
#include "move/move.h"

int main(void)
{
    bsp_init();

    NVIC_EnableIRQ(TIMER_0_INST_INT_IRQN);
    DL_TimerG_startCounter(TIMER_0_INST);


    motor_start(&motor_1);
    motor_start(&motor_2);

    // 初始化直线行驶功能
    float target_speed = 0.0;
    float base_speed=0.0;
    char oled_str[20];

    // NVIC_EnableIRQ(DELAY_TIMER_INST_INT_IRQN);
    // DL_Timer_startCounter(DELAY_TIMER_INST);
    led_on(&user_led);
    move_status = STRAIGHT;
    uint8_t Task = 1;
    uint8_t Task_status = 0;
    uint8_t Task1_N = 0;
    uint8_t turn_num = 0;
    while (1)
    {
        char ob[20];
        sprintf(ob, "Task: %d", Task);
        OLED_ShowString(0, 0, (uint8_t*)ob, 8);
        sprintf(ob, "Task_status: %d", Task_status);
        OLED_ShowString(0, 1, (uint8_t*)ob, 8);
        sprintf(ob, "Task1_N: %d", Task1_N);
        OLED_ShowString(0, 2, (uint8_t*)ob, 8);
        if (key_is_pressed(&key1))
        {
            delay_ms(100);
            if (key_is_pressed(&key1))
            {
                Task ++;
                if(Task > 7) Task=1;
            }
        }

        if (key_is_pressed(&key2))
        {
            delay_ms(100);
            if (key_is_pressed(&key2))
            {
                Task1_N ++;
                if(Task1_N > 5) Task1_N = 1;
            }
        }

        if (key_is_pressed(&key3))
        {
            Task_status = 1;
        }
        if (key_is_pressed(&key4))
        {
            target_speed = base_speed;
            delay_ms(500);
        }

        if(Task_status == 1)
        {
            char USART_TX[20];
            switch (Task) {
                case 1:
                    move_status = STRAIGHT;
                    line_move_init();
                    base_speed = 0.2f;
                    target_speed = base_speed;
                    delay_ms(500);
                    while(1){
                        if (move_status == STRAIGHT)
                        {
                            line_straight(&motor_1, &motor_2, target_speed);
                        }        
                        else
                        {
                            turn_imu(&motor_1, &motor_2, target_speed);
                            if(move_status == STRAIGHT)
                            {
                                turn_num++;
                                if(turn_num == Task1_N*4)
                                {
                                    motor_stop(&motor_1);
                                    motor_stop(&motor_2);
                                    break;
                                }
                            }
                        }
                        if (target_speed != 0 && target_speed < 0.4) 
                            target_speed += 0.00005;
                        }
                    break;
                case 2:
                    sprintf(USART_TX, "2");
                    usart_send_data(usart1.uartx,(uint8_t*)USART_TX, 1);
                    break;
                case 3:
                    sprintf(USART_TX, "3");
                    usart_send_data(usart1.uartx,(uint8_t*)USART_TX, 1);
                    break;
                case 4:
                    sprintf(USART_TX, "4");
                    usart_send_data(usart1.uartx,(uint8_t*)USART_TX, 1);
                    move_status = STRAIGHT_SLOW;
                    line_move_slow_init();
                    base_speed = 0.1f;
                    target_speed = base_speed;
                    delay_ms(500);
                    while(1){
                        if (move_status == STRAIGHT_SLOW)
                        {
                            line_straight_slow(&motor_1, &motor_2, target_speed);
                        }        
                        else
                        {
                            turn_imu_slow(&motor_1, &motor_2, target_speed);
                            if(move_status == STRAIGHT_SLOW)
                            {
                                turn_num++;
                                if(turn_num == Task1_N*4)
                                {
                                    motor_stop(&motor_1);
                                    motor_stop(&motor_2);
                                    break;
                                }
                            }

                        }
                        if (target_speed != 0 && target_speed < 0.25) 
                            target_speed += 0.00005;
                        }
                    break;
                case 5:
                    sprintf(USART_TX, "5");
                    usart_send_data(usart1.uartx,(uint8_t*)USART_TX, 1);
                    break;
                case 6:
                    sprintf(USART_TX, "6");
                    usart_send_data(usart1.uartx,(uint8_t*)USART_TX, 1);
                    break;
                case 7:
                    break;
                default:
                    break;
            }
            Task_status = 0;
        }

        // 使用直线行驶控制
//        move_straight_proccess(&motor_1, &motor_2, target_speed);
    }
}
